<!-- PCS实时有功功率 -->
<template>
	<Echart
		id="leftCenter1"
		:options="options"
		class="left_center_inner"
		ref="echarts"
	/>
</template>

<script>
import { getgraph } from "@/api/modules/home.js";

export default {
	props: ["info", "type"],
	computed: {
		isCollapse() {
			return this.$store.state.isCollapse;
		},
	},
	data() {
		return {
			options: {},
		};
	},
	watch: {
		info: {
			handler(newValue, oldValue) {
				if (newValue) {
					// 获取psc图
					let self = this;
					let xData = [];
					let ygList = [];
					let selfList = [];
					getgraph({ devId: newValue[0].id, sampleName: "active_power" }).then(
						(res) => {
							ygList[0] = {
								type: "line",
								symbol: "none",
								// areaStyle: {},
								data: [],
								name: newValue[0].cname,
							};
							selfList.push(newValue[0].cname);
							let data = res.data;
							for (let index = 0; index < data.length; index++) {
								let element = data[index];
								xData.push(element.date);
								ygList[0].data.push(
									element.graphHisDTO ? element.graphHisDTO.value : ""
								); //实时有效功率
							}
						}
					);
					if (newValue.length > 1) {
						getgraph({
							devId: newValue[1].id,
							sampleName: "active_power",
						}).then((res1) => {
							if (res1.code == 200) {
								ygList[1] = {
									type: "line",
									symbol: "none",
									// areaStyle: {},
									data: [],

									name: newValue[1].cname,
								};
								let data = res1.data;
								selfList.push(newValue[1].cname);
								for (let index = 0; index < data.length; index++) {
									let element = data[index];
									ygList[1].data.push(
										element.graphHisDTO ? element.graphHisDTO.value : ""
									); //实时有效功率
								}
							} else {
								this.$Message({
									text: res.msg,
									type: "warning",
								});
							}
						});
					}

					this.$nextTick(() => {
						// console.log(xData, ygList, this.info.cname);
						this.init(xData, ygList, selfList);
					});
				}
			},
			deep: true,
		},
		isCollapse(oldVal, newVal) {
			if (oldVal !== newVal) {
				this.$refs.echarts.resize();
			}
		},
	},
	methods: {
		init(xData, yData, dataY2name) {
			this.options = {
				legend: {
					x: "center", //可设定图例在左、右、居中
					y: "10px",
					data: dataY2name,
					textStyle: {
						fontSize: 12,
						color: "#F1F1F3",
					},
				},
				xAxis: {
					type: "category",
					data: xData,
					boundaryGap: false, // 不留白，从原点开始
					splitLine: {
						show: true,
						lineStyle: {
							color: "rgba(31,99,163,.2)",
						},
					},
					axisLine: {
						// show:false,
						lineStyle: {
							color: "rgba(31,99,163,.1)",
						},
					},
					axisLabel: {
						color: "#7EB7FD",
						fontWeight: "500",
					},
				},
				yAxis: {
					name: "单位 ：kW",
					nameTextStyle: {
						color: "#fff",
						fontSize: 12,
					},
					type: "value",
					splitLine: {
						show: true,
						lineStyle: {
							color: "rgba(31,99,163,.2)",
						},
					},
					axisLine: {
						lineStyle: {
							color: "rgba(31,99,163,.1)",
						},
					},
					axisLabel: {
						color: "#7EB7FD",
						fontWeight: "500",
					},
				},
				tooltip: {
					trigger: "axis",
					backgroundColor: "rgba(0,0,0,.6)",
					borderColor: "rgba(147, 235, 248, .8)",
					textStyle: {
						color: "#FFF",
					},
					formatter: function (params) {
						var result = params[0].name + "<br/>";
						params.forEach(function (item) {
							result += item.seriesName + ": " + item.value + "<br/>";
						});
						return result;
					},
				},
				grid: {
					//布局
					show: true,
					left: "40px",
					right: "30px",
					bottom: "10px",
					top: "48px",
					containLabel: true,
					borderColor: "#1F63A3",
				},
				series: yData,
			};
		},
	},
};
</script>
<style lang="scss" scoped></style>
